// 管理账号信息
const USERS_KEY = 'USERS_KEY';
const STATE_KEY = 'STATE_KEY';

const getUsers = function () {
    let ret = '';
    ret = uni.getStorageSync(USERS_KEY);
    if (!ret) {
        ret = '[]';
    }
    return JSON.parse(ret);
}

const User_paw = function(user,paw){
	let User_paw = []
	User_paw.push(user)
	User_paw.push(paw)
	uni.setStorageSync('User_paw',User_paw)
	// return User_paw
}

const Memory = function(memory){
	uni.setStorageSync('memory',memory)
}

const addUser = function (userInfo) {
    let users = getUsers();
    users.push({
        account: userInfo.account,
        password: userInfo.password
    });
    uni.setStorageSync(USERS_KEY, JSON.stringify(users));
}
const config = {
	version : '1.0.0',
	pages : "../../static/page/page2.png",
}
const _api_root = 'http://miyou.8396048.com/';
const api = {
	login: {
		getsalt: _api_root + 'api/login/getsalt',//请求登录
		check: _api_root + 'api/login/check_login',//登录状态
		login:  _api_root + 'api/login/login',//系统登录
		logo: _api_root + "api/login/logo",//获取网站图像
		forget: _api_root + 'api/login/repair',//修改密码
	},
	reg: {
		register: _api_root + 'api/login/register',//新用户注册
		code: _api_root + 'api/login/getcode',//获取验证码
		agree: _api_root + 'port/login/get_user_regagree',//获取注册协议
		help: _api_root + 'port/member/helpRegister',//帮助注册
	},
	main:{
		url: _api_root,//域名
		reservation: _api_root + 'api/order/reservation',//申请匹配
		recount: _api_root + 'api/order/getReCount',//获取预约数量
		user: _api_root + 'api/member/info',//获取会员信息
		reserlist: _api_root + 'api/order/reservationList',//我的密友等待匹配
		initiativelist: _api_root + 'api/order/reservationMemberList',//我的密友主动添加
		passivelist: _api_root + 'api/order/transactionMemberList',//我的密友被动添加
		index: _api_root + 'api/order/transactionMemberList',//交友大厅
		
		info: _api_root + "port/index/confInfo",//配置信息
		assets: _api_root + 'port/investment/index',//获取投资列表
		code: _api_root + 'port/member/verifycode',//获取验证码
		verification: _api_root + 'port/member/verificationAccount',//短信验证
		golden: _api_root + '',//入金记录
		popup: _api_root + "port/index/notice",//通知信息
	},
	close: {
		active: _api_root + "api/order/getReservationMemberInfo",//主动添加用户信息详情
		passive: _api_root + "api/order/getTransactionMemberInfo",//被动添加用户信息详情
		order: _api_root + 'api/Pay/payReservationOrder',//预约提交
		pay: _api_root + 'api/Pay/payTransactionOrder',//预约通过
	},
	user: {
		verified: _api_root + 'api/member/verified',//会员验证
		setver: _api_root + 'api/member/setverified',//提交会员验证
		setcode: _api_root + 'api/member/upImage',//提交QRCODE到后台
		team: _api_root + 'api/member/peopleInfo',//团队信息
		code: _api_root + 'api/member/scan',//推荐码
		setting: _api_root + 'api/member/memberInfo',//修改用户信息
		
		wallet: _api_root + 'port/member/wallet',//钱包地址修改
		passtwo: _api_root + 'port/member/changePwd2',//钱包密码修改
		realname: _api_root + 'port/member/realname',//实名验证
		friends: _api_root + "port/member/inviteFriends",//获取邀请二维码
		earning: _api_root + "port/member/incomeLog",//获取收益记录
	},
	integral: {
		transfer: _api_root + 'api/credit/transfer',//积分赠送
		credit: _api_root + 'api/member/credit',//获取会员积分或余额
		recharge: _api_root + 'api/credit/creditShow',//积分充值
		submit: _api_root + 'api/credit/credit',//积分充值
		withdrawal: _api_root + 'api/credit/toCredit1',//余额转积分
		total: _api_root + 'api/credit/commissionRecord',//佣金记录
		integral: _api_root + 'api/credit/creditRecord',//积分记录
	},
	center: {
		login: _api_root + 'api/safe/repairpwd',//修改登录密码
		trade: _api_root + 'api/safe/repairchange',//修改交易密码
		friend: _api_root + 'api/rule/rule',//密友规则
		system: _api_root + 'api/article/article',//系统公告
		submit: _api_root + 'api/trial/trialInfo',//提交申诉
		trial: _api_root + 'api/trial/trial',//获取申诉列表
		question: _api_root + 'api/question/question',//常见问题
		service: _api_root + 'api/service/service',//客服列表
	}
}

const auth = function(self, url, data, _success){
	var token = uni.getStorageSync('token');
	if (token == '') {
		uni.showToast({
			icon: 'none',
			title: '登錄已失效，請重新登錄'
		});
		
		uni.setStorageSync('token','');
		uni.reLaunch({
			url: '/pages/login/login',
		});
	}
	uni.request({
		url: url,
		method: 'GET',
		data: data,
		header: {
			'Authorization':uni.getStorageSync('token'),
		},
		success: res => {
			var res = res.data;
			if (res.code == 1) {
				_success(self, res);
	
			} else {
				if (res.code == 401) {
					uni.setStorageSync('token','');
					uni.showToast({
						icon: 'none',
						title: '登錄已失效，請重新登錄'
					});
					setTimeout(function(){
						
						uni.reLaunch({
							url: '/pages/login/login'
						});
					},2000);
				} else {
					uni.showToast({
						icon: 'none',
						title: res.info
					});
				}
			}
		},
		fail: res => {
			console.log(res)
			uni.showToast({
				icon: 'none',
				title: '网站消息获取错误'
			});
		},
		complete: (data) => {
			var data = data.data;
			if(data.code == 13){
				uni.showToast({
					icon: 'none',
					title: data.msg
				});
				this.removeToken();
				uni.showToast({
					icon: 'none',
					title: '登錄已失效，請重新登錄'
				});
				
				uni.reLaunch({
					url: '/pages/login/login'
				});
			}
		}
	});
}

const upload = function(self, url, data, path, name, _success) {
	if(data == '' || path == ''){
		uni.request({
			url: url,
			method: 'GET',
			data: data,
			header: {
				'Authorization':uni.getStorageSync('token'),
			},
			success: res => {
				var res = res.data;
				_success(self, res);
			},
		});
	}else{
		uni.uploadFile({
			url : url,
			filePath: path,
			name: name,
			formData: data,
			header: {
				'Authorization':uni.getStorageSync('token'),
			},
			success: function (res) {
				res = res.data;
				res = eval('(' + res + ')');
				_success(self, res);
			}
		});
	}
}

export default {
    getUsers,
	User_paw,
    addUser,
	config,
	Memory,
	upload,
	auth,
	api,
}
